package com.leyou.page.service.impl;

import com.leyou.common.constants.MQConstants;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.IPageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiuer
 * @description 商品详情页面展示
 * @create 2019/7/3 19:55
 */
@Service
@Slf4j
public class PageServiceImpl implements IPageService {
    public static final String pathName = "E:\\javaTool\\nginx-1.12.2\\html\\item";
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private SpringTemplateEngine templateEngine;

    @Override
    public Map<String, Object> loadingItemsBySpuId(Long spuId) {
        Map<String, Object> itemMap = new HashMap<>();
        //根据spuId查询spu包括spuDetail
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        //根据 得到的分类id集合 查询所有分类集合
        List<CategoryDTO> categories = itemClient.queryCategoryByIds(spuDTO.getCategoryIds());
        //根据品牌id查询品牌
        BrandDTO brand = itemClient.queryBrandByBid(spuDTO.getBrandId());
        //根据spuId查询 sku集合
        List<SkuDTO> skus = itemClient.selectSku(spuId);
        //根据 三级分类id查询规格组和规格参数
        List<SpecGroupDTO> specs = itemClient.querySpecGroupAndParams(spuDTO.getCid3());
        itemMap.put("categories", categories);
        itemMap.put("brand", brand);
        itemMap.put("skus", skus);
        itemMap.put("spuName", spuDTO.getName());
        itemMap.put("subTitle", spuDTO.getSubTitle());
        itemMap.put("detail", spuDTO.getSpuDetail());
        itemMap.put("specs", specs);
        return itemMap;
    }

    /**
     * 商品详情页面静态化处理
     *
     * @param id
     */
    @Override
    public void creatItemHtml(Long id) {
        //准备数据模型
        Context context = new Context();
        //调用之前加载好的方法
        context.setVariables(loadingItemsBySpuId(id));
        //准备文件路径
        File dirFile = new File(pathName);
        if (!dirFile.exists()) {
            if (!dirFile.mkdirs()) {
                //创建失败抛出异常
                log.error("【静态微服务】静态页面创造失败，创建静态页目录失败，目录地址是 {}",
                        dirFile.getAbsolutePath());
                throw new LyException(ExceptionEnum.FILE_WRITER_ERROR);
            }
        }
        File creatHtml = new File(dirFile, id + ".html");
        //准备输出流 try catch 会自动调用close方法来关闭流。
        try {
            PrintWriter printWriter = new PrintWriter(creatHtml, "utf-8");
            templateEngine.process("item", context, printWriter);
        } catch (IOException e) {
            log.error("【静态微服务】静态页面写入失败。商品spuId是 {}", id, e);
            throw new LyException(ExceptionEnum.FILE_WRITER_ERROR);
        }

    }

}


